function Rank(){
	this.baseUrl = 'http://api.shihou.tv/api/gift';
	this.receive_url = this.baseUrl + '/receive_rank?gift_id=';
	this.send_url = this.baseUrl + '/send_rank?gift_id=';
	this.gift_ids = [1088, 1087];//1087黑板擦、1088粉笔
	this.user_rank_pre_html = ['魅力园丁榜排名:', '最佳教学榜排名:', '三好学生榜排名:'];
	//“魅力园丁”榜对应收 “粉笔”
	//“最佳教学园丁”榜 对应收“黑板檫”
	this.init();
}
Rank.prototype={
	init: function(){
		var _this = this;
		//三好学生
		var receive_1087_map = new Map();
		var receive_1087_url = _this.send_url + _this.gift_ids[1];
		_this.requestRemoteDataJson(receive_1087_url, {}, function(result){
			if(_this.parseResult(result, 'error') == 0){
				var resultData = _this.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					receive_1087_map.set(_this.parseResult(user, "user", "id").toString(), user);
				}
			}
		}, false);
		
		var receive_1088_map = new Map();
		var receive_1088_url = _this.send_url + _this.gift_ids[0];
		_this.requestRemoteDataJson(receive_1088_url, {}, function(result){
			if(_this.parseResult(result, 'error') == 0){
				var resultData = _this.parseResult(result, 'data', 'list');
				for (var i = 0;i < resultData.length; i++) {
					var user = resultData[i];
					receive_1088_map.set(_this.parseResult(user, "user", "id").toString(), user);
				}
			}
		}, false);
		
		_this.log(receive_1088_map);
		
		var data_array = [];
		var i = 0;
		//遍历receive_1087_map 黑板擦收礼，将值和1088粉笔的值相加；如果不存在，则添加
		
		var resMap = new Map();
		receive_1087_map.forEach(function (item, key, mapObj) {
			resMap.set(key, item);
		});
		receive_1088_map.forEach(function (item, key, mapObj) {
			if(resMap.get(key) == undefined || resMap.get(key) == "undefined"){
				resMap.set(key, item);
			}else{
				var user_1087 = resMap.get(key);
				var total1087 = parseInt(user_1087["total"]);
				var total1088 = item["total"];
				user_1087.total = parseInt(total1087) + parseInt(total1088);
				resMap.set(key, user_1087);
			}
		});
		
		resMap.forEach(function (item, key, mapObj) {
			data_array[i] = item;
			i++;
		});
		
		//排序后得到排行榜三的最终结果
		var result = _this.sortarr(data_array);
		_this.sanhao_result = result;
	},
	setRankList: function(index, page){
		var _this = this;
		
		var data_array = [];
		var api_url = _this.receive_url + _this.gift_ids[index];
		if(index == 2){
			data_array = _this.sanhao_result;
		}else{
			_this.requestRemoteDataJson(api_url, {}, function(result){
				if(_this.parseResult(result, 'error') == 0){
					var resultData = _this.parseResult(result, 'data', 'list');
					for (var i = 0;i < resultData.length; i++) {
						var user = resultData[i];
						data_array[i] = user;
						if(i == 9){
							break;
						}
					}
				}
			}, false);
		}
		//给meili_yuanding_html、zuijia_jiaoxue_yuanding_html赋值
		var html = '';
		for(var j = 0;j < 10;j++){
			var data = data_array[j];
			var roomSkip = '';
			if(page == 0){
				roomSkip = _this.parseResult(data, 'user', 'room', 'jump');
			}else{
				roomSkip = 'http://event.m.shihou.tv/lion_module_share_temp/share?room_id='+_this.parseResult(data, 'user', 'room', 'id');
			}
			if(index == 2){
				roomSkip = 'javascript:void(0);';
				html += _this.setRankListHTML((j+1), index, roomSkip, _this.parseResult(data, 'user', 'nick_name'), _this.parseResult(data, 'user', 'avatar'), _this.parseResult(data, 'user', 'room', 'vdo_status'), '送出'+_this.parseResult(data, 'total')/10+'个');
			}else{
				html += _this.setRankListHTML((j+1), index, roomSkip, _this.parseResult(data, 'user', 'nick_name'), _this.parseResult(data, 'user', 'avatar'), _this.parseResult(data, 'user', 'room', 'vdo_status'), '收到'+_this.parseResult(data, 'total')/10+'个');
			}
		}
		$('.phbList ul').html(html);
	},
	setUserRank: function(index, uid){
		var _this = this;
		var rankHTML = _this.user_rank_pre_html[index];
		var rank = null;
		if(uid == "0"){
			$('#userRank').html(rankHTML);
			rankHTML += "未上榜";
			return;
		}
		if(index == 2){
			for(var i = 0;i < _this.sanhao_result.length;i++){
				var user = _this.sanhao_result[i];
				var user_id = _this.parseResult(user, "user", "id");
				if(user_id == uid){
					rank = (i+1);
				}
			}
		}else{
			var api_url = _this.receive_url + _this.gift_ids[index];
			_this.requestRemoteDataJson(api_url, {}, function(result){
				if(_this.parseResult(result, 'error') == 0){
					var resultData = _this.parseResult(result, 'data', 'list');
					for (var i = 0;i < resultData.length; i++) {
						var user = resultData[i];
						var user_id = _this.parseResult(user, "user", "id");
						if(user_id == uid){
							rank = (i+1);
						}
					}
				}
			}, false);	
		}
		
		if(rank == null)
			rankHTML += "未上榜";
		else
			rankHTML += rank;
		$('#userRank').html(rankHTML);
	},
	setRankListHTML: function(index, num, skipRoom, nick_name, avatar, kaibo_status, value){
		var _this = this;
		var html = '';
		var aClass = 'liveNo'+index;//名次class
		var avatarHTML = '';
		var zhiboHTML = '<img src="static/img/wkb.png"/>';
		var rankIcon = '';
		
		if(index < 4){
			rankIcon = '<img src="static/img/icon'+index+'.png" />';
		}else{
			rankIcon = index;
		}
		
		if(num == 2){
			zhiboHTML = '';
		}
		
		if(kaibo_status == 1){//如果开播
			zhiboHTML = '<img src="static/img/zb.png"/>';
		}
		html = '<li><a href="'+skipRoom+'">'+
               '    	<em class="icon1">'+rankIcon+'</em>'+
               '         <em class="icon"><img src="'+avatar+'"/></em>'+
               '         <p>'+
               '         	<span>'+nick_name+'</span>'+
               '             <em>'+value+'</em>'+
               '         </p>'+
               '         <span class="zhibo">'+zhiboHTML+'</span>'+
               '     </a></li>';		
		return html;
	},
	sortarr: function(arr){
		var _this = this;
		for(i=0;i<arr.length-1;i++){
	        for(j=0;j<arr.length-1-i;j++){
	        	if(arr[j] == undefined || arr[j] == 'undefined' || arr[j+1] == undefined || arr[j+1] == 'undefined')
	        		break;
	        	var total1 = arr[j]['total'];
	        	var total2 = arr[j+1]['total'];
	            if(total1 < total2){
	                var temp=arr[j];
	                arr[j]=arr[j+1];
	                arr[j+1]=temp;
	            }
	        }
	    }
	    return arr;
	},
	//获取url中的参数
    getUrlParam: function(name, url) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); //构造一个含有目标参数的正则表达式对象
        url = url || window.location.search.substr(1).match(reg);  //匹配目标参数
        if (url != null) return unescape(url[2]); return null; //返回参数值
    },
    getUrlParams: function(){
    	var url = window.location.href;  //匹配目标参数
    	if(url != null)
    		return url.substr(url.indexOf("?"), url.length);
    },
    parseResult:function(){//解析结果， 如果不存在则返回''
		var size = arguments.length;
		if(size == 0){
			return '';
		}
		var result = arguments[0];
		for(var i = 1; i < size; i++){
			if(result == undefined){
				return '';
			}
			var argu = arguments[i];
			if(i == size){
				return argu;
			}
			result = result[argu];
		}
		return result;
	},
	requestRemoteDataJson: function(url, data, callback, asyn){
		//this.logURL(url);
		//this.logParameter(data);
		$.ajax({
	        url: url,
	        type: 'GET',
	        async: asyn,
	        cache: false, 
	        dataType: "json",
	        data: data,
	        success: callback,
	        error: function (data) {
	        	var status = data.status;
	        	console.log(url + "获取数据失败!");
	        }
	    });
	},
	requestRemotePostDataJson: function(url, data, callback, asyn){
		//this.logURL(url);
		$.ajax({
	        url: url,
	        type: 'POST',
	        async: asyn,
	        cache: false, 
	        dataType: "json",
	        data: data,
	        success: callback,
	        error: function (data) {
	        	var status = data.status;
	        	console.log(url + "获取数据失败!");
	        }
	    });
	},
	log:function(data){
		console.log(data);
	},
	logURL:function(url){
		console.log("url --> "+url);
	},
	logParameter:function(data){
		console.log("parameter --> "+JSON.stringify(data));
	},
	logParameter:function(args, data){
		console.log(args+" --> "+JSON.stringify(data));
	},
	logResult:function(result){
		console.log("result --> "+JSON.stringify(result));
	}
}


